package com.example.triprec.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.triprec.R;
import com.example.triprec.common.Result;
import com.example.triprec.dao.CommentDao;
import com.example.triprec.entity.Scenic;
import com.gzone.university.adapter.ItemViewHolder;
import com.gzone.university.adapter.ViewAdapter;
import com.gzone.university.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

public class ScenicAdapter extends ViewAdapter<ScenicAdapter.ScenicViewHolder> {


    private List<Scenic> mScenicList = new ArrayList<>();
    private OnItemClickListener mListener;

    public ScenicAdapter(View view) {
        super(view);
    }

    @Override
    public ScenicViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_scenic, parent, false);
        return new ScenicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ScenicViewHolder holder) {
        Scenic scenic = mScenicList.get(holder.getAdapterPosition());
        //随机设置图片
        GlideUtils.load(holder.ivScenicImage, scenic.getImageResources()[0]);
        holder.tvScenicName.setText(scenic.getName());
        // 设置评分和位置
        Result<Double> result = CommentDao.getAverageScenicScore(scenic.getId());
        holder.tvRatingLocation.setText(String.format("%.1f  |  %s", result.getData(), scenic.getLocation()));
        holder.tvScenicPrice.setText("¥" + String.format("%.0f", scenic.getPrice()));
        holder.tvScenicDesc.setText(scenic.getDescription());
        holder.tvScenicOpening.setText(scenic.getOpening());

        // 设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(scenic, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mScenicList.size();
    }

    public void setData(List<Scenic> scenicList) {
        this.mScenicList = scenicList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Scenic scenic, int position);
    }

    public static class ScenicViewHolder extends ItemViewHolder {
        ImageView ivScenicImage;
        TextView tvScenicName;
        TextView tvRatingLocation;
        TextView tvScenicPrice;
        TextView tvScenicDesc;
        TextView tvScenicOpening;

        public ScenicViewHolder(@NonNull View itemView) {
            super(itemView);
            ivScenicImage = itemView.findViewById(R.id.iv_scenic_image);
            tvScenicName = itemView.findViewById(R.id.tv_scenic_name);
            tvRatingLocation = itemView.findViewById(R.id.tv_rating_location);
            tvScenicPrice = itemView.findViewById(R.id.tv_scenic_price);
            tvScenicDesc = itemView.findViewById(R.id.tv_scenic_desc);
            tvScenicOpening = itemView.findViewById(R.id.tv_scenic_opening);
        }
    }
} 